package ${controllerPackage};

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import ${impBeanPackage};
import ${impServicePackage};

@Controller("${controllerName?uncap_first}")  
@RequestMapping("/${beanName?uncap_first}") 
public class ${controllerName} {
	
	@Autowired
	private ${serviceName} ${serviceName?uncap_first};
	
	private Logger log=LoggerFactory.getLogger(this.getClass());
	
	@ResponseBody
	@RequestMapping(value="/udateOrAdd${beanName}",method=RequestMethod.POST)
    public Map<String,Object> save${beanName}(@RequestBody JSONObject json){
		${beanName} ${beanName?uncap_first}=new ${beanName}();
         <#list bean as b> 
	         <#if b.fieldType=="String">
	         ${beanName?uncap_first}.set${b.fieldName?cap_first}(json.getString("${b.fieldName}"));
	         <#elseif b.fieldType=="Date">
	         ${beanName?uncap_first}.set${b.fieldName?cap_first}(json.getDate("${b.fieldName}"));
	         <#elseif b.fieldType=="BigDecimal">
	         ${beanName?uncap_first}.set${b.fieldName?cap_first}(json.getBigDecimal("${b.fieldName}"));
	         </#if>
         </#list>		
	    int update=0;
	    if("*".equals(${beanName?uncap_first}.get${tablePK.fieldName?cap_first}())){
	    	update=${serviceName?uncap_first}.add${beanName}(${beanName?uncap_first});
	    }else{
	    	update=${serviceName?uncap_first}.update${beanName}(${beanName?uncap_first});
	    }
		Map<String,Object>result=new HashMap<String,Object>();
		result.put("result", update>0?"success":"error");
		result.put("message",update<=0?"更新出错！":"");
		return result;
    }
	
	@ResponseBody
	@RequestMapping(value="/del${beanName}",method=RequestMethod.POST)
    public Map<String,Object>del${beanName}(@RequestBody JSONObject json){
		${beanName} ${beanName?uncap_first}=new ${beanName}();
		${beanName?uncap_first}.set${tablePK.fieldName?cap_first}(json.getString("${tablePK.fieldName}"));
	    int update=${serviceName?uncap_first}.del${beanName}(${beanName?uncap_first});
		Map<String,Object>result=new HashMap<String,Object>();
		result.put("result", update>0?"success":"error");
		result.put("message",update<=0?"更新出错！":"");
		return result;
    }
	
	
	@RequestMapping(value="/manager",method=RequestMethod.GET)
    public ModelAndView manager(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/${beanName?uncap_first}/${beanName?uncap_first}Manager");
        return mv;
    }
	
	@ResponseBody
	@RequestMapping(value="/query${beanName}List",method=RequestMethod.POST)
    public  Map<String,Object> query${beanName}List(HttpServletRequest request){
		 String table = "${tableName}";
		 //获取请求次数
	    String draw = "0";
	    draw = request.getParameter("draw");
	    //数据起始位置
	    String start = request.getParameter("start");
	    //数据长度
	    String length = request.getParameter("length");
	    length=String.valueOf(Integer.parseInt(start)+Integer.parseInt(length));
	    start=String.valueOf(Integer.parseInt(start)+1);
	    //总记录数
	    String recordsTotal = "0";
	    //过滤后记录数
	    String recordsFiltered = "";
	    String[] cols = {"",<#list bean as b><#if b_has_next>"${b.columnName}",<#else>"${b.columnName}"</#if> </#list>};
	    //获取客户端需要那一列排序
	    String orderColumn = "0";
	    orderColumn = request.getParameter("order[0][column]");
	    orderColumn = cols[Integer.parseInt(orderColumn)];
	    //获取排序方式 默认为asc
	    String orderDir = "asc";
	    orderDir = request.getParameter("order[0][dir]");
	    
	    //获取用户过滤框里的字符
	    String searchValue = request.getParameter("search[value]");
	    
	    List<String> sArray = new ArrayList<String>();
	    if (!searchValue.equals("")) {
	        <#list bean as b> 
	          sArray.add(" ${b.columnName} like '%" + searchValue + "%'");
            </#list> 
	    }

	    String individualSearch = "";
	    if (sArray.size() == 1) {
	        individualSearch = sArray.get(0);
	    } else if (sArray.size() > 1) {
	        for (int i = 0; i < sArray.size() - 1; i++) {
	            individualSearch += sArray.get(i) + " or ";
	        }
	        individualSearch += sArray.get(sArray.size() - 1);
	    }

	        String recordsFilteredSql = "select count(1) as recordsFiltered from " + table;
	        //获取数据库总记录数
	        String recordsTotalSql = "select count(1) as recordsTotal from " + table;


	        String searchSQL = "";
	        String sql = "SELECT * FROM (SELECT tt.*, ROWNUM AS rowno"+
                                         " FROM (  SELECT t.* from "+ table+ " t";
	        if (individualSearch != "") {
	            searchSQL = " where " + individualSearch;
	        }
	        sql += searchSQL;
	        recordsFilteredSql += searchSQL;
	        sql += " order by " + orderColumn + " " + orderDir+" )tt WHERE ROWNUM <="+length+") ttt WHERE ttt.rowno >="+start;
	        recordsFilteredSql += " order by " + orderColumn + " " + orderDir;
        
	    return ${serviceName?uncap_first}.getPage(draw,recordsFilteredSql,recordsTotalSql,sql);
    }
}
